package com.fh.fruit.controller.version;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.fruit.entity.version.AppVersion;
import com.fh.fruit.service.version.VersionManager;
import com.fh.util.Jurisdiction;
import com.fh.util.PageData;

@Controller
@RequestMapping(value="/verion")
public class VersionController extends BaseController {
	
	String menuUrl = "verion/list.do"; //版本信息地址(权限用)
	@Resource(name="appVersionService")
	private VersionManager appVersionService;
	
	/**显示app版本信息列表
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	public ModelAndView listAppVersion(Page page,HttpSession httpSession)throws Exception{
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();	
		PageData pd=this.getPageData();
		page.setPd(pd);
		page.setShowCount(6);
		List<PageData> versionList=appVersionService.appVersionlistPage(page);
		mv.setViewName("farm/version/version_list");
		mv.addObject("versionList", versionList);
		mv.addObject("pd", pd);
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		System.out.println("权限=========="+Jurisdiction.getHC());
		return mv;
	}
	
	
	/**去新增版本信息页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/goAddVersion")
	public ModelAndView goAddVersion()throws Exception{
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		mv.setViewName("farm/version/version_edit");
		mv.addObject("msg", "saveVersion");
		return mv;
	}	
	
	/**新增版本信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/saveVersion",method = RequestMethod.POST)
	public ModelAndView saveVersion(AppVersion av,Page page,@RequestParam("pic") MultipartFile pic) throws Exception{
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		logBefore(logger, Jurisdiction.getUsername()+"新增版本信息");
		ModelAndView mv = this.getModelAndView();
		List<PageData> versionList=appVersionService.appVersionlistPage(page);
		if(versionList.size() >0){
			for (PageData pageData : versionList) {
				int code=(int)pageData.get("app_version_code");
				String type=(String)pageData.get("app_type");
				int code1=av.getCode();
				String type1=av.getType();
				if(code==code1 && type.equals(type1)){
					mv.addObject("msg","版本已存在!");
					mv.setViewName("save_result");
				}
			}
		}
		appVersionService.saveAppVersion(av,pic);
		mv.setViewName("save_result");
		return mv;
	}
	
	/**去修改版本信息页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/goEditAppVersion")
	public ModelAndView goEditAppVersion()throws Exception{
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd=this.getPageData();
	    PageData version= appVersionService.findById(pd);
		mv.setViewName("farm/version/version_edit");
		mv.addObject("version",version);
		mv.addObject("msg", "editAppVersion");
		return mv;
	}				
	
	/**修改版本信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/editAppVersion")
	public ModelAndView editAppVersion(AppVersion av,@RequestParam("pic") MultipartFile pic,HttpServletRequest requset) throws Exception{
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		logBefore(logger, Jurisdiction.getUsername()+"修改版本信息");
		ModelAndView mv = this.getModelAndView();
		appVersionService.editAppVersion(av,pic);
		mv.setViewName("save_result");
		return mv;
	}

	/**删除版本信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteVersion")
	@ResponseBody
	public String deleteVersion(HttpServletRequest requset) {
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "del")){return null;} //校验权限
		logBefore(logger, Jurisdiction.getUsername()+"删除版本信息");
		PageData pd =this.getPageData();
		try {
			appVersionService.deleteAppVersion(pd);
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			return "failed";
		}
	}

}
